package ym.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ym.mapper.RepairEvaluationMapper;
import ym.pojo.RepairEvaluation;
import ym.service.RepairEvaluationService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class RepairEvaluationServiceImpl implements RepairEvaluationService {
    @Autowired
    private RepairEvaluationMapper repairEvaluationMapper;

    @Override
    public void add(RepairEvaluation repairEvaluation) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String timeNow = sdf.format(new Date());
        repairEvaluation.setRepairEvaluationTime(timeNow);
        repairEvaluationMapper.insert(repairEvaluation);
    }

    @Override
    public void update(RepairEvaluation repairEvaluation) {
        repairEvaluationMapper.updateById(repairEvaluation);
    }

    @Override
    public void delete(Integer id) {
        repairEvaluationMapper.deleteById(id);
    }

    @Override
    public RepairEvaluation findById(Integer id) {
        return repairEvaluationMapper.selectById(id);
    }

    @Override
    public List<RepairEvaluation> findAll() {
        return repairEvaluationMapper.selectList(null);
    }

    @Override
    public Page<RepairEvaluation> search(String search, int page, int size) {
        QueryWrapper<RepairEvaluation> queryWrapper = new QueryWrapper<>();
        if (search != null) {
            // 可按需修改查询条件
            // queryWrapper.like("", search);
        }
        return repairEvaluationMapper.selectPage(new Page<>(page, size), queryWrapper);
    }

    @Override
    public Page<RepairEvaluation> searchByRepairReportId(Integer repairReportId, String search, int page, int size) {
        QueryWrapper<RepairEvaluation> queryWrapper = new QueryWrapper<>();
        if (search != null) {
            queryWrapper.like("repairReportId", repairReportId);
        }else {
            queryWrapper.eq("repairReportId", repairReportId);
        }
        return repairEvaluationMapper.selectPage(new Page<>(page, size), queryWrapper);
    }
}
